package edu.msg.jthr.web.servlets;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.Candidate;
import edu.msg.jthr.backend.model.Job;
import edu.msg.jthr.backend.model.RecommendedCandidate;
import edu.msg.jthr.backend.model.User;
import edu.msg.jthr.backend.service.JobService;
import edu.msg.jthr.backend.service.UserService;
import edu.msg.jthr.web.util.RoleAssembler;

/**
 * Servlet implementation class RecomandToJob
 */
@WebServlet("/RecommendToJob")
public class RecommendToJob extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	private JobService jobService;
	@EJB
	private UserService userService;
	private HttpSession session;
	private static Logger LOG = LoggerFactory.getLogger(RecommendToJob.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RecommendToJob() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		User user = (User) session.getAttribute("activeUser");
		if (user == null) {
			user = userService.getUserByUserName(request.getUserPrincipal()
					.toString());
			String role = RoleAssembler.getRole(user.getUserGroups());
			session.setAttribute("role", role);
			session.setAttribute("activeUser", user);
		}
		Candidate candidate = (Candidate) session.getAttribute("candidate");
		if (candidate == null) {
			String pagePath = "/ShowJobs";
			request.getRequestDispatcher(pagePath).forward(request, response);
		} else {
			List<Job> jobList = Collections.emptyList();
			try {
				jobList = jobService.getJobsWhereNotApplied(candidate);
			} catch(ServiceException e) {
				LOG.error(e.getMessage(), e);
			}
			session.setAttribute("jobList", jobList);
			request.getRequestDispatcher("/pages/RecommendToJob.jsp").forward(
					request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		Job job = new Job();
		try {
			job = jobService.getById(Long.parseLong(request
					.getParameter("jobId")));
		} catch (ServiceException  e) {
			LOG.error(e.getMessage(), e);
		}
		
		Candidate candidate = (Candidate) session.getAttribute("candidate");
		RecommendedCandidate recommendedCandidate = new RecommendedCandidate(
				job, candidate);
		job.getRecomendedCandidates().add(recommendedCandidate);
		try {
			jobService.update(job);
		} catch(ServiceException e) {
			LOG.error(e.getMessage(), e);
		}
		
		doGet(request, response);
	}

}
